<?php
namespace App\Controller;
use Think\Controller;
use App\Controller\OrgBaseController;
class OrgtestController extends OrgBaseController {

    /**
     * 测试数据
     * @author xuan
     * @param id midiID
     */
	public function test(){
		$video_arr = M('video')->find();
		echo '<pre>';
		print_r($video_arr);
		exit();
        echo "测试";die;
    }

    /**
     * 用户登录
     * @author xuan
     * @param org_phone       机构登录号码（必填）
     * @param org_password    机构登录密码（必填）
     * @param org_version     机构端版本code（必填）
     */
    public function login(){
        // $json_arr = $this->Security(false);

        $json_arr['org_version'] = 101;
        $json_arr['org_phone'] = '13811341066';
        $json_arr['org_password'] = '111111';
        if (empty($json_arr['org_phone']) || empty($json_arr['org_password'])) {
            $this->backToClient1(2,'参数不全');
        }

        //版本控制（强制更新） 1.未传版本号   2.版本号小于最新版本号
        $org_version = M('version')->where('version_client = 3')->order('version_code desc')->find();
        if (empty($json_arr['org_version'])) {
            $this->backToClient1(300,'请更新到最新版本');
        }elseif ($json_arr['org_version'] < $org_version['version_code']) {
            $this->backToClient1(300,'请更新到最新版本');
        }

        $json_arr['org_password'] = md5($json_arr['org_password']);
        if($club_arr = M('midiclub')->where(['midiclub_admin'=>$json_arr['org_phone'],'midiclub_password'=>$json_arr['org_password']])->find()){
            if (empty($club_arr['midiclub_sig'])) {
                $data['midiclub_sig'] = $this->getUserSig('club'.$club_arr['midiclub_id']);
                //usersig过期时间是生成以后的150天
                $data['midiclub_sigexpire'] = time() + 60*60*24*150;
            }
            $data['midiclub_last_time'] = time();
            $data['midiclub_id'] = $club_arr['midiclub_id'];
            $data['midiclub_token'] = createRandom();
            M('midiclub')->save($data);
            $result = M('midiclub')->where(['midiclub_id'=>$club_arr['midiclub_id']])->find();
            unset($result['midiclub_password']);
            $this->backToClient1(1,'登录成功',$result);
        }else{
            $this->backToClient1(103,'账号或密码错误');
        }
    }

    /**
     * 忘记密码
     * @author xuan
     * @param phone    手机号码（必填）
     * @param password 密码（必填）
     */
    public function forgetPassword(){
        // $json_arr = $this->Security();
        $json_arr['phone'] = '13811341066';
        $json_arr['password'] = 111111;
        if (empty($json_arr['phone']) || empty($json_arr['password'])) {
            $this->backToClient1(2,'参数不全');
        }
        $condition['midiclub_admin'] = $json_arr['phone'];
        $midiclub = M('midiclub')->field('midiclub_id')->where($condition)->find();
        if (empty($midiclub)) {
            $this->backToClient1(104,'该手机号码未被注册');
        }
        $midiclub['midiclub_password'] = md5($json_arr['password']);
        M('midiclub')->save($midiclub);
        $this->backToClient1(1,'重置密码成功！');

    }

    /**
     * 修改密码
     * @author xuan
     * @param id               机构id（必填）
     * @param password         密码（必填）
     * @param newpassword      新密码（必填）
     */
    public function updatePassword(){
        // $json_arr = $this->Security();
        $json_arr['id'] = '1';
        $json_arr['password'] = 111112;
        $json_arr['newpassword'] = 111111;
        if (empty($json_arr['id']) || empty($json_arr['password']) || empty($json_arr['newpassword'])) {
            $this->backToClient1(2,'参数不全');
        }
        $data['midiclub_id'] = $json_arr['id'];
        $midiclub = M('midiclub')->field('midiclub_id,midiclub_password')->where($data)->find();
        if (empty($midiclub)) {
            $this->backToClient1(3,'机构id不存在');
        }else{
            if (md5($json_arr['password']) == $midiclub['midiclub_password']) {
                $midiclub['midiclub_password'] = md5($json_arr['newpassword']);
                if (M('midiclub')->save($midiclub)) {
                    $this->backToClient1(1,'修改密码成功');
                }else{
                    $this->backToClient1(4,'新密码和旧密码重复');
                }
            }else{
                $this->backToClient1(5,'原始密码错误');
            }
        }

    }

    /**
     * 获取机构信息
     * @author xuan
     * @param org_id       机构id（必传）
     */
    public function getOrgInfo(){
        // $json_arr = $this->Security();
        $json_arr['org_id'] = 1;
        if (empty($json_arr['org_id'])) {
            $this->backToClient1(2,'参数不全');
        }
        $midiclub = M('midiclub')->where(['midiclub_id'=>$json_arr['org_id']])->find();
        if (!empty($midiclub)) {
            if ($midiclub['midiclub_display'] == 0) {
                $this->backToClient1(4,'该机构账号已经被禁用');
            }else{
                unset($midiclub['midiclub_password']);
                $this->backToClient1(1,'获取机构信息成功',$midiclub);
            }
        }else{
            $this->backToClient1(3,'机构id不存在');
        }

    }

    /**
     * 修改机构信息
     * @author xuan
     * @param midiclub_id               机构id（必传）
     * @param midiclub_name             机构名称（选填）
     * @param midiclub_phone1           联系方式1（选填）
     * @param midiclub_phone2           联系方式2（选填）
     * @param midiclub_phone3           联系方式3（选填）
     * @param midiclub_address          地址（选填）
     * @param midiclub_city             城市（选填）
     * @param midiclub_pic              封面图（选填）
     * @param midiclub_content          介绍（选填）
     */
    public function editOrgInfo(){
        // $json_arr = $this->Security();
        $json_arr['midiclub_id'] = 1;
        $json_arr['midiclub_phone1'] = '吉他张老师 13811341066';
        if (empty($json_arr['midiclub_id'])) {
            $this->backToClient1(2,'参数不全');
        }
        M('midiclub')->save($json_arr);
        $this->backToClient1(1,'修改机构信息成功！');

    }

    /**
     * 申请一场线下考试
     * @author xuan
     * @param examorg_orgid                 机构id（必传）
     * @param examorg_guitar                科目电吉他（必传）
     * @param examorg_drum                  科目爵士鼓（必传）
     * @param examorg_bass                  科目电贝司（必传）
     * @param examorg_date                  考试日期（Y-m-d）（必传）
     * @param examorg_time                  考试时段 上午1 下午2 全天3（必传）
     * @param examorg_prenum                预估人数（必传）
     */
    public function applyOfflineExam(){
        // $json_arr = $this->Security();
        $json_arr['examorg_orgid'] = 1;
        $json_arr['examorg_guitar'] = 1;
        $json_arr['examorg_drum'] = 1;
        $json_arr['examorg_bass'] = 1;
        $json_arr['examorg_date'] = '2018-03-18';
        $json_arr['examorg_time'] = 3;
        $json_arr['examorg_prenum'] = 50;
        if (empty($json_arr['examorg_orgid']) || empty($json_arr['examorg_date']) || empty($json_arr['examorg_time']) || empty($json_arr['examorg_prenum'])) {
            $this->backToClient1(2,'参数不全');
        }elseif(strtotime($json_arr['examorg_date']) < time()){
            $this->backToClient1(7,'申请时间不能小于当前时间');
        }
        //判断当天是否有考试
        $condition['examorg_orgid'] = $json_arr['examorg_orgid'];
        $condition['examorg_date'] = $json_arr['examorg_date'];
        if ($json_arr['examorg_time'] == 1) {
            $condition['examorg_time'] = array('IN','1,3');
        }elseif ($json_arr['examorg_time'] == 2) {
            $condition['examorg_time'] = array('IN','2,3');
        }elseif ($json_arr['examorg_time'] == 3) {
            $condition['examorg_time'] = array('IN','1,2,3');
        }else{
            $this->backToClient1(3,'考试时间段错误！');
        }
        $examorg = M('examorg')->where($condition)->find();
        if (empty($examorg)) {  //当天无考试
            //查看是否有考官接收考试
            if ($json_arr['examorg_guitar'] == 1) {
                $condition['examorg_options_guitar'] = 1;
            }
            if ($json_arr['examorg_drum'] == 1) {
                $condition['examorg_options_drum'] = 1;
            }
            if ($json_arr['examorg_bass'] == 1) {
                $condition['examorg_options_bass'] = 1;
            }
            $condition['examorg_options_date'] = $json_arr['examorg_date'];
            if ($json_arr['examorg_time'] == 1) {
                $condition['examorg_options_allowtime'] = array('IN','1,3');
            }elseif ($json_arr['examorg_time'] == 2) {
                $condition['examorg_options_allowtime'] = array('IN','2,3');
            }elseif ($json_arr['examorg_time'] == 3) {
                $condition['examorg_options_allowtime'] = 3;
            }
            $examiner_option = M('examorg_options')->where($condition)->order('rand()')->find();
            if (empty($examiner_option)) {
                $this->backToClient1(4,'本次申请考试时间段无考官');
            }else{
                //查询预计人数百分比
                $option = M('option')->find();
                //匹配考官
                $json_arr['examorg_examinerid'] = $examiner_option['examorg_options_userid'];
                $json_arr['examorg_maxnum'] = $json_arr['examorg_prenum'] * (1 + $option['option_premax']/100);
                $json_arr['examorg_createtime'] = time();
                $json_arr['examorg_deadline'] = strtotime($json_arr['examorg_date'].' 23:00:00');
                if ($examorg_id = M('examorg')->add($json_arr)) {
                	//将考官的设置时间修改
                	M('examorg_options')->where(['examorg_options_id'=>$examiner_option['examorg_options_id']])->setDec('examorg_options_allowtime',$json_arr['examorg_time']);
                	//写入房间号
                	M('examorg')->save(['examorg_id'=>$examorg_id,'examorg_roomid'=>(1000000+$examorg_id)]);
                    $this->backToClient1(1,'申请考试成功');
                }else{
                    $this->backToClient1(5,'申请考试失败');
                }
            }
        }else{  //当天有考试
        	$this->backToClient1(6,'当天有考试，不能申请考试');
        }
    }

    /**
     * 获取考试列表（分页）
     * @author xuan
     * @param orgid       机构id（必填）
     * @param status      考试状态 1未考完 2已考完（必填）
     */
    public function orgExamList(){
        // $json_arr = $this->Security();
        $json_arr['orgid'] = 3;
        $json_arr['status'] = 1;
        if (empty($json_arr['orgid']) || empty($json_arr['status'])) {
            $this->backToClient1(2,'参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $condition['examorg_orgid'] = $json_arr['orgid'];
        $condition['examorg_status'] = $json_arr['status'];
        $examorg_arr = M('examorg')->where($condition)->order('examorg_createtime desc')->page($p,$pnum)->select();
        $this->backtoclient1(1,'获取考试列表成功',$examorg_arr);
    }

    /**
     * 获取考试学生列表（分页）
     * @author xuan
     * @param examorgid             机构考试id（必填）
     * @param idcard_num            身份证号码（选填）
     */
    public function orgExamStudentList(){
        // $json_arr = $this->Security();
        $json_arr['examorgid'] = 23;
        // $json_arr['idcard_num'] = 1;
        if (empty($json_arr['examorgid'])) {
            $this->backToClient1(2,'参数不全');
        }
        $p = (!empty($json_arr['p']))?$json_arr['p']:1;
        $pnum = (!empty($json_arr['pnum']))?$json_arr['pnum']:10;
        $condition['examorg_enroll_examorgid'] = $json_arr['examorgid'];
        if (!empty($json_arr['idcard_num'])) {
            $condition['user_idcard'] = $json_arr['idcard_num'];
        }
        $examorg_enroll_arr = M('examorg_enroll')->field('e.examorg_enroll_id,e.examorg_enroll_avatar,e.examorg_enroll_major,e.examorg_enroll_level,u.user_id,u.user_name,u.user_idcard')->alias('e')->join('left join midi_user as u on e.examorg_enroll_userid = u.user_id')->where($condition)->page($p,$pnum)->select();
        $this->backtoclient1(1,'获取考试学生列表成功',$examorg_enroll_arr);
    }

    /**
     * 获取学生详情
     * @author xuan
     * @param userid                考生id（必填）
     */
    public function studentDetails(){
        // $json_arr = $this->Security();
        $json_arr['userid'] = 23;
        if (empty($json_arr['userid'])) {
            $this->backToClient1(2,'参数不全');
        }
        $user_arr = M('user')->field('user_id,user_avatar,user_name,user_nickname,user_level_guitar,user_level_drum,user_level_bass,user_sex,user_nation,user_birthday,user_phone,user_email,user_idcard,user_idpic1')->where(['user_id'=>$json_arr['userid']])->find();
        $this->backtoclient1(1,'获取学生详情成功',$user_arr);
    }

    /**
     * 学生完成机构考试
     * @author xuan
     * @param examorg_enroll_id                考试报名id（必填）
     */
    public function studentFinishOrgExam(){
        // $json_arr = $this->Security();
        $json_arr['examorg_enroll_id'] = 6;
        if (empty($json_arr['examorg_enroll_id'])) {
            $this->backToClient1(2,'参数不全');
        }
        $condition['examorg_enroll_id'] = $json_arr['examorg_enroll_id'];
        $examorg_enroll_arr = M('examorg_enroll')->where($condition)->find();
        if (!empty($examorg_enroll_arr)) {
            if ($examorg_enroll_arr['examorg_enroll_status'] == 3) {
                $this->backtoclient1(4,'该场考试已经结束！');
            }else{
                $condition['examorg_enroll_status'] = 3;
                $condition['examorg_enroll_finish_time'] = time();
                M('examorg_enroll')->save($condition);
                $this->backtoclient1(1,'学生完成机构考试！');
            }
        }else{
            $this->backtoclient1(3,'该场考试不存在！');
        }
    }

    /**
     * 机构端考试开关（超过每日限定时间点无法启动开关）
     * @author xuan
     * @param examorg_id                考试报名id（必填）
     * @param examorg_status            考试状态 1开启考试 2结束考试（必填）
     */
    public function orgExamSwitch(){
        // $json_arr = $this->Security();
        $json_arr['examorg_id'] = 6;
        $json_arr['examorg_status'] = 2;
        if (empty($json_arr['examorg_id']) || empty($json_arr['examorg_status'])) {
            $this->backToClient1(2,'参数不全');
        }
        $condition['examorg_id'] = $json_arr['examorg_id'];
        $examorg_arr = M('examorg')->where($condition)->find();
        if ($examorg_arr['examorg_deadline'] < time()) {
            $this->backToClient1(3,'超过每日限定时间点无法启动开关');
        }else{
            if ($examorg_arr['examorg_status'] == 1) {
                if ($json_arr['examorg_status'] == 1) {
                    $this->backToClient1(5,'考试未结束');
                }elseif ($json_arr['examorg_status'] == 2) {
                    $condition['examorg_status'] = 2;
                    M('examorg')->save($condition);
                    $this->backToClient1(1,'结束考试成功！');
                }else{
                    $this->backToClient1(7,'考试状态参数错误！');
                }
            }elseif ($examorg_arr['examorg_status'] == 2) {
                if ($json_arr['examorg_status'] == 2) {
                    $this->backToClient1(6,'考试已结束');
                }elseif ($json_arr['examorg_status'] == 1) {
                    $condition['examorg_status'] = 1;
                    M('examorg')->save($condition);
                    $this->backToClient1(1,'开启考试成功！');
                }else{
                    $this->backToClient1(7,'考试状态参数错误！');
                }
            }else{
                $this->backToClient1(4,'考试状态错误！');
            }
        }
    }

}